package algorithm.array;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @Author: Hevery
 * @Description:
 * @Date: Created on 2017/10/3
 * @Modified By:
 */
public class BeautifulNumber {
    public static void main(String[] args) {
        Scanner scan = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int cases = scan.nextInt();
        for (int i = 1; i <= cases; i++) {
            int n = scan.nextInt();
            System.out.println(beautiful(n));
        }



    }
    private static int beautiful(int n){
        for(int radix = 2; radix < n; radix++){
            if(isBeautiful(n,radix)){
                return radix;
            }
        }
        return n-1;
    }
    private static boolean isBeautiful(int n,int radix){
        while(n > 0){
            if(n % radix != 1){
                return false;
            }
            n /= radix;
        }
        return true;
    }
}
